# Star.py
from enum import Enum
from typing import List, Optional


# 假设存在以下枚举类型，需要根据实际定义补充
class YinYangEnum(Enum):
    阳 = 1
    阴 = 2


class StarCatelogEnum(Enum):
    杂星 = 0
    主星 = 1
    副星 = 2
    四化星 = 3
    # 根据实际需要添加其他星类枚举值

# Star.py
from enum import Enum
from typing import List, Optional, Dict, Any


class YinYangEnum(Enum):
    阳 = 1
    阴 = 2


class StarCatelogEnum(Enum):
    杂星 = 0
    主星 = 1
    副星 = 2
    四化星 = 3
    六吉星 = 4
    六煞星 = 5


class Star:
    """
    星类
    """

    # 使用字典存储星曜的模板数据，避免在类定义时创建对象实例
    _STAR_TEMPLATES: Dict[str, Dict[str, Any]] = {
        # 十四主星 - 北斗星
        "紫微": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.主星,
            "WuXingAttr": "土",
            "YinYangAttr": YinYangEnum.阴,
            "StarTypes": [StarCatelogEnum.主星],
        },
        "贪狼": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.主星,
            "WuXingAttr": "木",
            "YinYangAttr": YinYangEnum.阳,
            "StarTypes": [StarCatelogEnum.主星],
        },
        "巨门": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.主星,
            "WuXingAttr": "水",
            "YinYangAttr": YinYangEnum.阴,
            "StarTypes": [StarCatelogEnum.主星],
        },
        "廉贞": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.主星,
            "WuXingAttr": "火",
            "YinYangAttr": YinYangEnum.阴,
            "StarTypes": [StarCatelogEnum.主星],
        },
        "武曲": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.主星,
            "WuXingAttr": "金",
            "YinYangAttr": YinYangEnum.阴,
            "StarTypes": [StarCatelogEnum.主星],
        },
        "破军": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.主星,
            "WuXingAttr": "水",
            "YinYangAttr": YinYangEnum.阴,
            "StarTypes": [StarCatelogEnum.主星],
        },
        # 十四副星
        "天马": {
            "StarLevel": 2,
            "PrimaryCatelog": StarCatelogEnum.副星,
            "WuXingAttr": "火",
            "YinYangAttr": YinYangEnum.阳,
            "StarTypes": [StarCatelogEnum.副星],
        },
        "禄存": {
            "StarLevel": 2,
            "PrimaryCatelog": StarCatelogEnum.副星,
            "WuXingAttr": "土",
            "YinYangAttr": YinYangEnum.阴,
            "StarTypes": [StarCatelogEnum.副星],
        },
        # 十四主星 - 南斗星
        "天府": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.主星,
            "WuXingAttr": "土",
            "YinYangAttr": YinYangEnum.阳,
            "StarTypes": [StarCatelogEnum.主星],
        },
        "天相": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.主星,
            "WuXingAttr": "水",
            "YinYangAttr": YinYangEnum.阳,
            "StarTypes": [StarCatelogEnum.主星],
        },
        "天梁": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.主星,
            "WuXingAttr": "土",
            "YinYangAttr": YinYangEnum.阳,
            "StarTypes": [StarCatelogEnum.主星],
        },
        "天同": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.主星,
            "WuXingAttr": "水",
            "YinYangAttr": YinYangEnum.阳,
            "StarTypes": [StarCatelogEnum.主星],
        },
        "天机": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.主星,
            "WuXingAttr": "木",
            "YinYangAttr": YinYangEnum.阴,
            "StarTypes": [StarCatelogEnum.主星],
        },
        "七杀": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.主星,
            "WuXingAttr": "金",
            "YinYangAttr": YinYangEnum.阳,
            "StarTypes": [StarCatelogEnum.主星],
        },
        # 十四主星 - 中天主星
        "太阳": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.主星,
            "WuXingAttr": "火",
            "YinYangAttr": YinYangEnum.阳,
            "StarTypes": [StarCatelogEnum.主星],
        },
        "太阴": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.主星,
            "WuXingAttr": "水",
            "YinYangAttr": YinYangEnum.阴,
            "StarTypes": [StarCatelogEnum.主星],
        },
        # 六吉星
        "左辅": {
            "StarLevel": 2,
            "PrimaryCatelog": StarCatelogEnum.六吉星,
            "WuXingAttr": "土",
            "YinYangAttr": YinYangEnum.阳,
            "StarTypes": [StarCatelogEnum.六吉星],
        },
        "右弼": {
            "StarLevel": 2,
            "PrimaryCatelog": StarCatelogEnum.六吉星,
            "WuXingAttr": "水",
            "YinYangAttr": YinYangEnum.阴,
            "StarTypes": [StarCatelogEnum.六吉星],
        },
        "文昌": {
            "StarLevel": 2,
            "PrimaryCatelog": StarCatelogEnum.六吉星,
            "WuXingAttr": "金",
            "YinYangAttr": YinYangEnum.阴,
            "StarTypes": [StarCatelogEnum.六吉星],
        },
        "文曲": {
            "StarLevel": 2,
            "PrimaryCatelog": StarCatelogEnum.六吉星,
            "WuXingAttr": "水",
            "YinYangAttr": YinYangEnum.阴,
            "StarTypes": [StarCatelogEnum.六吉星],
        },
        "天魁": {
            "StarLevel": 2,
            "PrimaryCatelog": StarCatelogEnum.六吉星,
            "WuXingAttr": "火",
            "YinYangAttr": YinYangEnum.阳,
            "StarTypes": [StarCatelogEnum.六吉星],
        },
        "天钺": {
            "StarLevel": 2,
            "PrimaryCatelog": StarCatelogEnum.六吉星,
            "WuXingAttr": "火",
            "YinYangAttr": YinYangEnum.阴,
            "StarTypes": [StarCatelogEnum.六吉星],
        },
        # 六煞星
        "火星": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.六煞星,
            "WuXingAttr": "火",
            "YinYangAttr": YinYangEnum.阳,
            "StarTypes": [StarCatelogEnum.六煞星],
        },
        "铃星": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.六煞星,
            "WuXingAttr": "火",
            "YinYangAttr": YinYangEnum.阴,
            "StarTypes": [StarCatelogEnum.六煞星],
        },
        "擎羊": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.六煞星,
            "WuXingAttr": "金",
            "YinYangAttr": YinYangEnum.阳,
            "StarTypes": [StarCatelogEnum.六煞星],
        },
        "陀罗": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.六煞星,
            "WuXingAttr": "金",
            "YinYangAttr": YinYangEnum.阴,
            "StarTypes": [StarCatelogEnum.六煞星],
        },
        "地劫": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.六煞星,
            "WuXingAttr": "火",
            "YinYangAttr": YinYangEnum.阳,
            "StarTypes": [StarCatelogEnum.六煞星],
        },
        "地空": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.六煞星,
            "WuXingAttr": "火",
            "YinYangAttr": YinYangEnum.阴,
            "StarTypes": [StarCatelogEnum.六煞星],
        },
        # 中天杂星
        "三台": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.杂星,
        },
        "八座": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.杂星,
        },
        "龙池": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.杂星,
        },
        "风阁": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.杂星,
        },
        "寡宿": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.杂星,
        },
        "孤辰": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.杂星,
        },
        "天官": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.杂星,
        },
        "天福": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.杂星,
        },
        "天虚": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.杂星,
        },
        "天哭": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.杂星,
        },
        "天喜": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.杂星,
        },
        "红鸾": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.杂星,
        },
        "天德": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.杂星,
        },
        "月德": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.杂星,
        },
        "天伤": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.杂星,
        },
        "天使": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.杂星,
        },
        "恩光": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.杂星,
        },
        "天贵": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.杂星,
        },
        "台辅": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.杂星,
        },
        "封诰": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.杂星,
        },
        "截空": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.杂星,
        },
        "旬空": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.杂星,
        },
        "天厨": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.杂星,
        },
        "天寿": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.杂星,
        },
        "天刑": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.杂星,
        },
        "蜚廉": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.杂星,
        },
        "阴煞": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.杂星,
        },
        "解神": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.杂星,
        },
        "破碎": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.杂星,
        },
        "天巫": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.杂星,
        },
        "天月": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.杂星,
        },
        "天才": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.杂星,
        },
        "天姚": {
            "StarLevel": 1,
            "PrimaryCatelog": StarCatelogEnum.杂星,
        },
    }

    # 延迟初始化的星库
    _XING_KU: Optional[List["Star"]] = None

    def __init__(self):
        self.StarName: str = ""  # 星名
        self.StarLevel: int = 1  # 星级别
        self.SiHuaXing: str = ""  # 四化星
        self.WuXingAttr: str = ""  # 五行属性
        self.LightLevel: str = ""  # 星亮度
        self.YinYangAttr: YinYangEnum = None  # 阴阳属性
        self.PrimaryCatelog: StarCatelogEnum = None  # 主要属性
        self.StarTypes: List[StarCatelogEnum] = []  # 其他附属属性

    @classmethod
    def _initialize_xing_ku(cls):
        """初始化星库，只执行一次"""
        if cls._XING_KU is not None:
            return  # 已初始化则跳过

        cls._XING_KU = []
        for name, attrs in cls._STAR_TEMPLATES.items():
            star = cls()
            star.StarName = name
            star.StarLevel = attrs.get("StarLevel", 1)
            star.PrimaryCatelog = attrs.get("PrimaryCatelog", StarCatelogEnum.杂星)
            star.WuXingAttr = attrs.get("WuXingAttr", "")
            star.YinYangAttr = attrs.get("YinYangAttr")
            star.StarTypes = attrs.get("StarTypes", [])
            star.SiHuaXing = ""
            cls._XING_KU.append(star)

    @classmethod
    def get_xing_ku(cls) -> List["Star"]:
        """获取星库"""
        cls._initialize_xing_ku()
        return cls._XING_KU

    @classmethod
    def find_xing_by_name(cls, name: str) -> "Star":
        """
        根据名称查找星曜对象
        :param name: 星曜名称
        :return: 匹配的Star对象，若未找到则抛出异常
        """
        cls._initialize_xing_ku()
        for star in cls._XING_KU:
            if star.name == name:
                return star
        raise ValueError(f"未找到名为 '{name}' 的星曜")

    @staticmethod
    def CreateStar(StarName: str, level: int = 1) -> "Star":
        """
        实例化一个星

        Args:
            StarName: 星名
            level: 星级别，默认为1

        Returns:
            Star: 创建的星实例
        """
        star = Star()
        star.StarName = StarName
        star.StarLevel = level

        try:
            template = Star.find_xing_by_name(StarName)
            star.WuXingAttr = template.WuXingAttr
            star.YinYangAttr = template.YinYangAttr
            star.PrimaryCatelog = template.PrimaryCatelog
            star.StarTypes = template.StarTypes[:]
            star.SiHuaXing = ""
        except ValueError:
            star.PrimaryCatelog = StarCatelogEnum.杂星

        return star

    @property
    def name(self) -> str:
        return self.StarName
